#coding=utf-8
import uliweb.form as form

class ReqForm(form.Form):
    def post_html(self):
        from uliweb.core.html import Script
        from uliweb.core.js import json_dumps
        from uliweb import settings
        
        buf = """
<script>
    jQuery.fn.extend({
        selectLink:function(ids,id,JSON){
            $(ids).change(function(){
                var b = [];html = '';
                $(id).html(html);
                var c = $(ids).val();
                b = JSON[c];
                if(b != ''){
                    for(var j in b){html = html + '<option value=' + b[j][0] + '>' + b[j][1] + '</option>'}
                    $(id).html(html);
                }
            })
        }
    }); 
    //js页面
    $(document).ready(function(){
        $().selectLink('#field_mode','#field_stage',stage)
    });
    var stage = %s;
</script>
    """ % json_dumps(settings.PARA.REQ_STAGE)
        
        return buf

class ReqNewstageForm(form.Form):
    def form_validate(self, data):
        from datetime import datetime
        error = {}
        
        if data['newstage_begin_date']<data['end_date']:
            error['newstage_begin_date'] =  '下一阶段开始日期不能小于上一阶段结束日期'
        if data['end_date']<data['begin_date']:
            error['end_date'] = '上一阶段结束日期不能小于上一阶段开始日期'
        if data['end_date'] > datetime.date(datetime.now()) :
            error['end_date'] = '上一阶段结束日期不能大于当前日期'
        return error

class ReqDetailsEditForm(form.Form):
    def form_validate(self, data):
        from datetime import datetime
        error = {}
        
        if data['end_date']<data['begin_date']:
            error['end_date'] = '本阶段结束日期不能小于本阶段开始日期'
        if data['end_date'] > datetime.date(datetime.now()) :
            error['end_date'] = '本阶段结束日期不能大于当前日期'
        return error
